home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cnetsess.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  7.1 KB  |  289 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. IMPLEMENT_SERIAL( CNetworkSessions, CNetwork, 1 )
  18.  
  19. CNetworkSessions::CNetworkSessions()
  20. {
  21.    m_Initialize();
  22. }
  23.  
  24. CNetworkSessions::CNetworkSessions( LPCTSTR machine_name )
  25. {
  26.    Open( machine_name );
  27. }
  28.  
  29. CNetworkSessions::~CNetworkSessions()
  30. {
  31.    Close();
  32. }
  33.  
  34. void CNetworkSessions::Close( void )
  35. {
  36.    CNetwork::Close();
  37.    m_Initialize();
  38. }
  39.  
  40. BOOL CNetworkSessions::Delete( CNetworkSessionInformation& information )
  41. {
  42.    WCHAR wide_user_name[ 256 ];
  43.    WCHAR wide_client_name[ 256 ];
  44.  
  45.    ::ZeroMemory( wide_user_name,   sizeof( wide_user_name   ) );
  46.    ::ZeroMemory( wide_client_name, sizeof( wide_client_name ) );
  47.  
  48.    LPWSTR user   = (LPWSTR) NULL;
  49.    LPWSTR client = (LPWSTR) NULL;
  50.  
  51.    if ( information.UserName.GetLength() > 0 )
  52.    {
  53. #if ! defined( UNICODE )
  54.       ::strcpy( (LPSTR) wide_user_name, information.UserName );
  55.       ::ASCII_to_UNICODE( (LPSTR) wide_user_name, wide_user_name );
  56. #else
  57.       ::strcpy( wide_user_name, information.UserName );
  58. #endif // UNICODE
  59.  
  60.       user = wide_user_name;
  61.    }
  62.  
  63.    if ( information.ClientName.GetLength() > 0 )
  64.    {
  65. #if ! defined( UNICODE )
  66.       ::strcpy( (LPSTR) wide_client_name, information.ClientName );
  67.       ::ASCII_to_UNICODE( (LPSTR) wide_client_name, wide_client_name );
  68. #else
  69.       ::strcpy( client_user_name, information.ClientName );
  70. #endif // UNICODE
  71.  
  72.       client = wide_client_name;
  73.    }
  74.  
  75.    m_ErrorCode = ::NetSessionDel( (LPTSTR) m_WideMachineName,
  76.                                   (LPTSTR) client,
  77.                                   (LPTSTR) user );
  78.    
  79.    if ( m_ErrorCode == NERR_Success )
  80.    {
  81.       return( TRUE );
  82.    }
  83.    else
  84.    {
  85.       return( FALSE );
  86.    }
  87. }
  88.  
  89. BOOL CNetworkSessions::Enumerate( CNetworkSessionInformation& information )
  90. {
  91.    SESSION_INFO_502 *information_p = (SESSION_INFO_502 *) NULL;
  92.  
  93.    SESSION_INFO_502 s;
  94.  
  95.    ::ZeroMemory( &s, sizeof( s ) );
  96.  
  97.    information_p = &s;
  98.  
  99.    DWORD prefered_maximum_length = sizeof( SESSION_INFO_502 ) * 128;
  100.    DWORD number_of_entries_read  = 0;
  101.    DWORD total_number_of_entries = 0;
  102.  
  103.    m_ErrorCode = ::NetSessionEnum( (LPTSTR) m_WideMachineName, 
  104.                                    (LPTSTR) NULL, 
  105.                                    (LPTSTR) NULL, 
  106.                                             502,
  107.                                 (LPBYTE *) &information_p,
  108.                                             prefered_maximum_length,
  109.                                            &number_of_entries_read,
  110.                                            &total_number_of_entries,
  111.                                            &m_ResumeHandle );
  112.  
  113.    if ( information_p != (SESSION_INFO_502 *) NULL )
  114.    {
  115.       information.Copy( information_p );
  116.       return( TRUE );
  117.    }
  118.  
  119.    return( FALSE );
  120. }
  121.  
  122. BOOL CNetworkSessions::GetNext( CNetworkSessionInformation& information )
  123. {
  124.    return( Enumerate( information ) );
  125. }
  126.  
  127. void CNetworkSessions::m_Get_0_Data( void )
  128. {
  129.    LPBYTE buffer = (LPBYTE) NULL;
  130.  
  131.    /*
  132.    ** One of the children got loose in the header files again...
  133.    **
  134.    ** Also, we can't get 101 information because it doesn't work if you supply
  135.    ** a machine name... Go Figure...
  136.    */
  137.  
  138.    ::NetSessionGetInfo( (LPTSTR) m_WideMachineName, NULL, NULL, 0, &buffer );
  139.  
  140.    if ( buffer != NULL )
  141.    {
  142.       SESSION_INFO_0 *information_p = (SESSION_INFO_0 *) buffer;
  143.  
  144. #if ! defined( UNICODE )
  145.       ::UNICODE_to_ASCII( (LPCWSTR) information_p->sesi0_cname, information_p->sesi0_cname );
  146. #endif
  147.  
  148.       /*
  149.       ** Now store the info we want...
  150.       */
  151.  
  152.       m_ServerName = information_p->sesi0_cname;
  153.       m_Retrieved0 = TRUE;
  154.    }
  155. }
  156.  
  157. void CNetworkSessions::m_Get_1_Data( void )
  158. {
  159. }
  160.  
  161. void CNetworkSessions::m_Get_2_Data( void )
  162. {
  163. }
  164.  
  165. void CNetworkSessions::m_Get_10_Data( void )
  166. {
  167. }
  168.  
  169. void CNetworkSessions::m_Get_502_Data( void )
  170. {
  171. }
  172.  
  173. void CNetworkSessions::m_Initialize( void )
  174. {
  175.    m_ServerName.Empty();
  176.    m_ClientName.Empty();
  177.  
  178.    m_Retrieved0   = FALSE;
  179.    m_Retrieved1   = FALSE;
  180.    m_Retrieved2   = FALSE;
  181.    m_Retrieved10  = FALSE;
  182.    m_Retrieved502 = FALSE;
  183.  
  184.    m_ErrorCode    = 0;
  185.    m_ResumeHandle = 0;
  186. }
  187.  
  188. void CNetworkSessions::Serialize( CArchive& archive )
  189. {
  190.    CNetwork::Serialize( archive );
  191. }
  192.  
  193. /*
  194. ** CSessionInformation stuff
  195. */
  196.  
  197. IMPLEMENT_SERIAL( CNetworkSessionInformation, CObject, 1 )
  198.  
  199. CNetworkSessionInformation::CNetworkSessionInformation()
  200. {
  201.    m_Initialize();
  202. }
  203.  
  204. CNetworkSessionInformation::~CNetworkSessionInformation()
  205. {
  206.    m_Initialize();
  207. }
  208.  
  209. void CNetworkSessionInformation::Copy( SESSION_INFO_502 *source )
  210. {
  211.    ASSERT( source != NULL );
  212.  
  213.    if ( source == NULL )
  214.    {
  215.       m_Initialize();
  216.       return;
  217.    }
  218.  
  219. #if ! defined( UNICODE )
  220.    ::UNICODE_to_ASCII( (LPCWSTR) source->sesi502_cname,       source->sesi502_cname       );
  221.    ::UNICODE_to_ASCII( (LPCWSTR) source->sesi502_username,    source->sesi502_username    );
  222.    ::UNICODE_to_ASCII( (LPCWSTR) source->sesi502_cltype_name, source->sesi502_cltype_name );
  223.    ::UNICODE_to_ASCII( (LPCWSTR) source->sesi502_transport,   source->sesi502_transport   );
  224. #endif
  225.  
  226.    ClientName     = source->sesi502_cname;
  227.    UserName       = source->sesi502_username;
  228.    ClientTypeName = source->sesi502_cltype_name;
  229.    Transport      = source->sesi502_transport;
  230.    NumberOfOpens  = source->sesi502_num_opens;
  231.    Time           = CTimeSpan( source->sesi502_time      );
  232.    IdleTime       = CTimeSpan( source->sesi502_idle_time );
  233.    UserFlags      = source->sesi502_user_flags;
  234.  
  235. #if ! defined( UNICODE )
  236.    ::ASCII_to_UNICODE( source->sesi502_cname,       (LPWSTR) source->sesi502_cname       );
  237.    ::ASCII_to_UNICODE( source->sesi502_username,    (LPWSTR) source->sesi502_username    );
  238.    ::ASCII_to_UNICODE( source->sesi502_cltype_name, (LPWSTR) source->sesi502_cltype_name );
  239.    ::ASCII_to_UNICODE( source->sesi502_transport,   (LPWSTR) source->sesi502_transport   );
  240. #endif
  241. }
  242.  
  243. void CNetworkSessionInformation::Empty( void )
  244. {
  245.    m_Initialize();
  246. }
  247.  
  248. void CNetworkSessionInformation::m_Initialize( void )
  249. {
  250.    ClientName.Empty();
  251.    UserName.Empty();
  252.    ClientTypeName.Empty();
  253.    Transport.Empty();
  254.    NumberOfOpens = 0;
  255.    Time          = CTimeSpan( 0 );
  256.    IdleTime      = CTimeSpan( 0 );
  257.    UserFlags     = 0;
  258. }
  259.  
  260. void CNetworkSessionInformation::Serialize( CArchive& archive )
  261. {
  262.    CObject::Serialize( archive );
  263.  
  264.    if ( archive.IsStoring() )
  265.    {
  266.       archive << ClientName;
  267.       archive << UserName;
  268.       archive << NumberOfOpens;
  269.       archive << Time;
  270.       archive << IdleTime;
  271.       archive << UserFlags;
  272.       archive << ClientTypeName;
  273.       archive << Transport;
  274.    }
  275.    else
  276.    {
  277.       archive >> ClientName;
  278.       archive >> UserName;
  279.       archive >> NumberOfOpens;
  280.       archive >> Time;
  281.       archive >> IdleTime;
  282.       archive >> UserFlags;
  283.       archive >> ClientTypeName;
  284.       archive >> Transport;
  285.    }
  286. }
  287.  
  288.  
  289.